package com.example.server.controller;

import com.example.server.entity.User;
import com.example.server.service.UserService;
import com.example.server.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @PostMapping
    public String login(@RequestBody User loginRequest) {
        User user = userService.getUserByUsername(loginRequest.getUsername());
        if (user != null && user.getPassword() == loginRequest.getPassword()) {
            return JwtUtil.generateToken(user.getUsername());
        } else {
            throw new RuntimeException("用户名或密码错误");
        }
    }
}
